package com.yygo.model;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by tiansha on 2015/6/23.
 */
@Entity
@Table(name = "vehicle_series")
public class VehicleSeries extends BaseObject {
    private static final long serialVersionUID = 861116545703284767L;
    private Long id;
    private String name;
    private VehicleBrand vehicleBrand;
    private List<VehicleModel> vehicleModels = new ArrayList<VehicleModel>();

    public VehicleSeries() {}

    public VehicleSeries(String name) {
        this.name = name;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(length = 50)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @ManyToOne
    @JoinColumn(name="brand_id")
    public VehicleBrand getVehicleBrand() {
        return vehicleBrand;
    }

    public void setVehicleBrand(VehicleBrand vehicleBrand) {
        this.vehicleBrand = vehicleBrand;
    }

    @OneToMany(fetch = FetchType.EAGER,mappedBy = "vehicleSeries", cascade = CascadeType.REMOVE)
    @Fetch(FetchMode.SELECT)
    public List<VehicleModel> getVehicleModels() {
        return vehicleModels;
    }

    public void setVehicleModels(List<VehicleModel> vehicleModels) {
        this.vehicleModels = vehicleModels;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        VehicleSeries vehicleSeries = (VehicleSeries) o;

        if (name.equals(vehicleSeries.name)) {
            return true;
        }

        return false;
    }

    public int hashCode() {
        int result;
        result = (name != null ? name.hashCode() : 0);
        return result;
    }

    public String toString() {
        return "VehicleSeries{" +
                "id=" + id +
                ", name='" + name +
                '}';
    }
}
